<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleCreate"> 新增</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon style="fontSize:12px" icon="ant-design:down-outlined"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)"/>
      </template>
    </BasicTable>
    <DetailModal @register="register" />
    <MchDrawer @register="registerMchDrawer" @success="reload" />
    <PayIfConfigDrawer @register="registerPayIfConfig" @success="reload" />
  </div>
</template>
<script lang="ts" name="PayMchAppList" setup>
import {BasicTable, TableAction} from '/@/components/Table';
import {useDrawer} from '/@/components/Drawer';
import DetailModal from './DetailModal.vue';
import MchDrawer from './MchDrawer.vue';
import PayIfConfigDrawer from './PayIfConfigListDrawer.vue';
import {useGlobSetting} from '/@/hooks/setting';
import {columns, searchFormSchema} from './mchApp.data';
import {batchDeleteMchApp, deleteMchApp, listMchApp} from './mchApp.api';
import {useListPage} from '/@/hooks/system/useListPage';
import {useRouter} from "vue-router"
import {useMessage} from '/@/hooks/web/useMessage';
import {Icon} from '/@/components/Icon'

const { createMessage } = useMessage();
const router = useRouter()
let mchNo = router.currentRoute.value.query?.mchNo
console.log("currentRoute.params , mchNo: " + mchNo);
mchNo = mchNo == undefined ? '' : mchNo

const glob = useGlobSetting();
const [register, { openDrawer: openDetail }] = useDrawer();
const [registerMchDrawer, { openDrawer: openMchDrawer }] = useDrawer();
const [registerPayIfConfig, { openDrawer: openPayIfConfigDrawer }] = useDrawer();
// 列表页面公共参数、方法
const { prefixCls, tableContext } = useListPage({
  designScope: 'PayMchAppList',
  tableProps: {
    title: '商户应用列表',
    beforeFetch: (params) => {
      params.mchNo = !params.mchNo ? mchNo : params.mchNo
    },
    api: listMchApp,
    columns: columns,
    bordered: false,
    actionColumn: {
      width: 185,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      slots: { customRender: 'action' },
    },
    formConfig: {
      schemas: searchFormSchema,
      fieldMapToTime: [['fieldTime', ['beginDate', 'endDate'], 'YYYY-MM-DD HH:mm:ss']],
    },
  }
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

/**
 * 查看
 */
function handleDetail(record) {
  openDetail(true, {
    record,
    isUpdate: true,
  });
}
/**
 * 编辑
 */
function handleEdit(record: Recordable) {
  openMchDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handlePayIfConfig(record: Recordable) {
  openPayIfConfigDrawer(true, {
    record,
  });
}

/**
 * 操作列定义
 * @param record
 */
function getActions(record) {
  return [
    {
      label: '删除',
      color: 'error',
      ifShow: record.sendStatus != 1,
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      },
    },
    {
      label: '查看',
      onClick: handleDetail.bind(null, record),
      // ifShow: record.sendStatus == 0
    },
    {
      label: '配置',
      // 支付接口配置
      onClick: handlePayIfConfig.bind(null, record),
      // ifShow: record.sendStatus == 0
    },
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
      // ifShow: record.sendStatus == 0
    }
  ]
}

/**
 * 删除
 */
const handleDelete = async (record) => {
  await deleteMchApp({ mchNo: record.mchNo, appId: record.id }, () => {
    reload()
    createMessage.success("删除成功")
  })
  
}
/**
 * 批量删除事件
 */
 async function batchHandleDelete() {
  await batchDeleteMchApp({ ids: selectedRowKeys.value }, () => {
    reload()
    createMessage.success("删除成功")
  });
}


/**
 * 新增
 */
 function handleCreate() {
  openMchDrawer(true, {
    mchNo,
    isUpdate: false,
  });

  
}

</script>
